package pm25;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class city {

	public static void main(String[] args) {
		List<String> nopm = new ArrayList<String>();
		String[] arr = {"北京","上海","天津","重庆","香港","澳门","台北","高雄","台中",
				"哈尔滨","齐齐哈尔","牡丹江","佳木斯","绥化","黑河","大兴安岭","伊春","大庆","七台河","鸡西","鹤岗","双鸭山",
				"长春","吉林","延边","四平","通化","白城","辽源","松原","白山","沈阳","大连","鞍山","抚顺","本溪","丹东","锦州",
				"营口","阜新","辽阳","铁岭","朝阳","盘锦","葫芦岛","呼和浩特","包头","乌海","乌兰察布","通辽","赤峰","鄂尔多斯",
				"巴彦淖尔","锡林郭勒","呼伦贝尔","乌兰浩特","阿拉善盟","石家庄","保定","张家口","承德","唐山","廊坊","沧州","衡水",
				"邢台","邯郸","秦皇岛","郑州","安阳","新乡","许昌","平顶山","信阳","南阳","开封","洛阳","商丘","焦作","鹤壁","濮阳",
				"周口","漯河","驻马店","三门峡","济源","太原","大同","阳泉","晋中","长治","晋城","临汾","运城","朔州","忻州","吕梁",
				"济南","青岛","淄博","德州","烟台","潍坊","济宁","泰安","临沂","菏泽","滨州","东营","威海","枣庄","日照","莱芜","聊城",
				"南京","无锡","镇江","苏州","南通","扬州","盐城","徐州","淮安","连云港","常州","泰州","宿迁","杭州","湖州","嘉兴","宁波",
				"绍兴","台州","温州","丽水","金华","衢州","舟山","福州","厦门","宁德","莆田","泉州","漳州","龙岩","三明","南平","南昌","九江",
				"上饶","抚州","宜春","吉安","赣州","景德镇","萍乡","新余","鹰潭","合肥","蚌埠","芜湖","淮南","马鞍山","安庆","宿州","阜阳","亳州",
				"黄山","滁州","淮北","铜陵","宣城","六安","池州","武汉","襄阳","鄂州","孝感","黄冈","黄石","咸宁","荆州","宜昌","恩施","十堰","神农架",
				"随州","荆门","天门","仙桃","潜江","长沙","湘潭","株洲","衡阳","郴州","常德","益阳","娄底","邵阳","岳阳","张家界","怀化","永州","湘西",
				"广州","韶关","惠州","梅州","汕头","深圳","珠海","佛山","肇庆","湛江","江门","河源","清远","云浮","潮州","东莞","中山","阳江","揭阳",
				"茂名","汕尾","南宁","崇左","柳州","来宾","桂林","梧州","贺州","贵港","玉林","百色","钦州","河池","北海","防城港","海口","三亚","东方",
				"临高","澄迈","儋州","昌江","白沙","琼中","定安","屯昌","琼海","文昌","保亭","万宁","陵水","西沙","南沙","乐东","五指山","中沙","贵阳",
				"遵义","安顺","黔南","黔东南","铜仁","毕节","六盘水","黔西南","昆明","大理","红河","曲靖","保山","文山","玉溪","楚雄","普洱","昭通","临沧",
				"怒江","迪庆","丽江","德宏","西双版纳","成都","攀枝花","自贡","绵阳","南充","达州","遂宁","广安","巴中","泸州","宜宾","内江","资阳","乐山",
				"眉山","凉山","雅安","甘孜","阿坝","德阳","广元","拉萨","日喀则","山南","林芝","昌都","那曲","阿里","西安","咸阳","延安","榆林","渭南","商洛",
				"安康","汉中","宝鸡","铜川","杨凌","银川","石嘴山","吴忠","固原","中卫","兰州","定西","平凉","庆阳","武威","金昌","张掖","酒泉","天水","陇南",
				"临夏","甘南","白银","嘉峪关","西宁","海东","黄南","海南","果洛","玉树","海西","海北","格尔木","乌鲁木齐","克拉玛依","石河子","昌吉","吐鲁番",
				"巴音郭楞","阿拉尔","阿克苏","喀什","伊犁","塔城","哈密","和田","阿勒泰","克州","博尔塔拉"};
	     for(String cityName : arr){
//	       测试汉字是否转码
	 		try {
	 			cityName=URLEncoder.encode(cityName, "UTF-8");
	 		} catch (UnsupportedEncodingException e1) {
	 			e1.printStackTrace();
	 		}
	 		String airNowUrl = "http://api.thinkpage.cn/v3/air/now.json?key=QN2ZD5D6JN&location=" + cityName + "&language=zh-Hans&scope=city";
	 		String result = "";
	 		BufferedReader reader = null;
	 		try {
	 		    URL realurl = new URL(airNowUrl);
	 		    URLConnection connection = realurl.openConnection();
	 		    connection.connect();
	 		    reader = new BufferedReader(new InputStreamReader(
	 		       connection.getInputStream(), "UTF-8"));
	 		    String line;
	 		    while ((line = reader.readLine()) != null) {
	 		       result += line;
	 		    }
	 		} catch (IOException e) {
	 		    e.printStackTrace();
	 		} finally {
	 			if (reader != null) {
	 				try {
	 					reader.close();
	 				} catch (IOException e) {
	 					e.printStackTrace();
	 				}
	 		    }
	 		}
	 		
	 		try {
				JSONObject airNowObject = JSONObject.fromObject(result); 
				if (airNowObject.has("results")) {
					JSONArray results = airNowObject.getJSONArray("results");
					JSONObject result1 = results.getJSONObject(0);
					JSONObject air = result1.getJSONObject("air");
					JSONObject city = air.getJSONObject("city");
					String pm25 = city.getString("pm25");
					String aqi = city.getString("aqi");
					String quality = city.getString("quality");
				} else {
					
				}
			} catch (Exception e) {
				e.printStackTrace();
				try {
					cityName = URLDecoder.decode(cityName,"UTF-8");
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
				}
				nopm.add(cityName);
			}
	     }    

	 System.out.println(nopm.toString());

	}

}
